<?php
require_once 'core/utils/repository.inc';
require_once 'services/TIntrinsicPageService.inc';
require_once 'models/acl.inc';
require_once 'util/agent.inc';

class TShowCaseService extends TIntrinsicPageService implements IItemNavigation {
/**
 * @var IKeyValueRepository
 */
	protected $_ioc_shop_settings_;

/**
 * @var IUserAgentDetector
 */	
	protected $_ioc_u_a_detector_;
	
	protected $_conf_mobile_app_;
	protected $_conf_tablet_app_;
	
	private $_menus_;
	
	protected function handleRequest(TRequest $request){
		if ($this->UADetector && ($this->MobileApp || $this->TabletApp)){
			if ($this->UADetector->IsMobile() && $this->MobileThemeName)
				$this->Application()->Redirect($this->MobileApp);
			if ($this->UADetector->IsTablet() && $this->TabletThemeName)
				$this->Application()->Redirect($this->TabletApp);
		}
		if ($themename = $this->ShopSettings->GetValue('Theme'))
			$this->ThemeEngine->ThemeName = $themename;
		parent::handleRequest($request);
	}	
	
	public function __get($nm){
		switch ($nm){
			case 'User':return $this->service->Acl->CurrentUser();break;
			case 'CatalogRoot':return $this->ShopSettings->GetValue('CatalogRoot');break;
			case 'MainMenuRoot':return $this->ShopSettings->GetValue('MainMenuRoot');break;
			default:return parent::__get($nm);break;
		}
	}
	
	private function _get_menus(){
		if (!$this->_menus_)
			$this->_menus_ = TConvertions::ConvertToArray($this->ShopSettings->GetValue('Menus'),array(';','='),array(false,true));
		return $this->_menus_; 
	}
	
	public function MenuSlug($menu){
		$menus = $this->_get_menus();
		if (isset($menus[$menu]))
			return $menus[$menu];
		return null;
	}
	
	public function IsMenu($slug){
		if ($slug == $this->MainMenuRoot)
			return true;
		$menus = $this->_get_menus();
		return in_array($slug,$menus);		
	}
	
	public function RawItemNavigationUrl($class,$id,$slug = null, $controller = null,$method = null, array $parameters = array()){
		$page = null;
		switch ($class){
			case 'TMSCategory':$page = 'category';break;
			case 'TMSGlossaryItemPage':
			case 'TMSPage':$page = 'content';break;
			case 'TMSCategorizedProductPage':
			case 'TMSArticlePage':
			case 'TMSNomenclaturePage':
			case 'TMSProductPage':$page = 'product';break;
			case 'TMSService':
			case 'TMSGoods':
			case 'TMSArticle':{
				return $this->Url('product',$controller,$method,array('product'=>$id) + $parameters);
			}break;
		}
		return $this->Url($page,$controller,$method,array('id'=>$this->Sef?$slug:$id) + $parameters);
	}
	
	public function ItemNavigationUrl(IItem $item, $controller = null, $method = null, array $parameters = array()){
		return $this->RawItemNavigationUrl($item->ClassName(), $item->ItemId(), $item->Slug, $controller, $method, $parameters);
	}
	
	public function SearchUrl(){
		return $this->Url('search', null, null);
	}
	
	public function ProfileUrl(){
		return $this->Url('profile');
	}
	
	public function RegisterUrl(){
		return $this->Url('register');
	}

	public function DeliveryUrl(){
		return $this->Url('content',null,null,array("id"=>"delivery"));
	}	
	
	public function AuthUrl(){
		return $this->Url('login');
	}
	
	public function VCardUrl(){
		return $this->Url('vcard');
	}
	
	public function QRCodeUrl(){
		return $this->Url('qrcode');
	}
	
	public function CartUrl(){
		return $this->Url('cart');
	}
	
	public function OrderUrl(){
		return $this->Url('order');
	}		
}
